package com.huabo.contract.oracle.mapper;

import com.hbfk.util.DateUtil;
import com.hbfk.util.PageInfo;
import com.huabo.contract.oracle.entity.TblLegalLitigationsettlement;
import org.apache.commons.lang.StringUtils;

public class TblLegalLitigationsettlementMapperSqlConfig {


    public String findListByPageInfo(PageInfo<TblLegalLitigationsettlement> pageInfo, TblLegalLitigationsettlement litigation,Integer disputeid) {
        StringBuffer sqlSb = new StringBuffer("SELECT * FROM (SELECT BUDGET.*,ROWNUM RN " +
                "FROM (SELECT * FROM TBL_LEGAL_LITIGATIONSETTLEMENT TLL\n" +
                "LEFT JOIN TBL_LEGAL_DISPUTREGISTRATION TLD ON TLL.DISPUTEINFO = TLD.DISPUTEID\n" +
                "WHERE TLL.LINKORG = "+ litigation.getLinkorg() );

        if (disputeid != null){
            sqlSb.append(" AND TLD.DISPUTEID = " + disputeid );
        }
        if (litigation.getFirstcourt() != null) {
            sqlSb.append(" AND TLL.FIRSTCOURT like '%" + litigation.getFirstcourt() + "%' ");
        }
        if (litigation.getPresidingjudge() != null) {
            sqlSb.append(" AND TLL.PRESIDINGJUDGE like '%" + litigation.getPresidingjudge() + "%' ");
        }
        if (!StringUtils.isBlank(litigation.getStartdate())) {
            sqlSb.append(" and TLL.DEALDATE >= to_date('" + litigation.getStartdate() + "','yyyy-mm-dd') ");
        }

        if (!StringUtils.isBlank(litigation.getEnddate())) {
            sqlSb.append(" and TLL.DEALDATE <= to_date('" + litigation.getEnddate() + "','yyyy-mm-dd') ");
        }

        sqlSb.append(" ORDER BY TLL.LITIGATIONID DESC) BUDGET WHERE rownum <= "+(pageInfo.getCurrentPage()*pageInfo.getPageSize())+" ) WHERE RN > "+pageInfo.getCurrentRecord());
        String sql = sqlSb.toString();
        return sql;
    }


    public String findListByPageInfoCount(TblLegalLitigationsettlement litigation,Integer disputeid) {
        StringBuffer sqlSb = new StringBuffer("SELECT COUNT(*) FROM TBL_LEGAL_LITIGATIONSETTLEMENT TLL\n" +
                "LEFT JOIN TBL_LEGAL_DISPUTREGISTRATION TLD ON TLL.DISPUTEINFO = TLD.DISPUTEID\n" +
                "WHERE TLL.LINKORG = "+ litigation.getLinkorg() );
        if (disputeid != null){
            sqlSb.append(" AND TLD.DISPUTEID = " + disputeid );
        }
        if (litigation.getFirstcourt() != null) {
            sqlSb.append(" AND TLL.FIRSTCOURT like '%" + litigation.getFirstcourt() + "%' ");
        }
        if (litigation.getPresidingjudge() != null) {
            sqlSb.append(" AND TLL.PRESIDINGJUDGE like '%" + litigation.getPresidingjudge() + "%' ");
        }


       // sqlSb.append(" ORDER BY litigationId DESC");
        String sql = sqlSb.toString();
        return sql;
    }


    public String saveLitigationSettlement(TblLegalLitigationsettlement litigation) {
        StringBuffer column = new StringBuffer("INSERT INTO TBL_LEGAL_LITIGATIONSETTLEMENT (LITIGATIONID");
        StringBuffer value = new StringBuffer(" VALUES (CIRCULATION_SEQUENCE.nextval");

        if(litigation.getFirstcourt() != null && !"".equals(litigation.getFirstcourt())) {
            column.append(",FIRSTCOURT");
            value.append(",'"+litigation.getFirstcourt()+"'");
        }
        if(litigation.getPresidingjudge() != null && !"".equals(litigation.getPresidingjudge())) {
            column.append(",PRESIDINGJUDGE");
            value.append(",'"+litigation.getPresidingjudge()+"'");
        }
        if(litigation.getCollegialpanel() != null && !"".equals(litigation.getCollegialpanel())) {
            column.append(",COLLEGIALPANEL");
            value.append(",'"+litigation.getCollegialpanel()+"'");
        }
        if(litigation.getLitigationamount() != null && !"".equals(litigation.getLitigationamount())) {
            column.append(",LITIGATIONAMOUNT");
            value.append(",'"+litigation.getLitigationamount()+"'");
        }
        if(litigation.getDealdate() != null && !"".equals(litigation.getDealdate())) {
            column.append(",DEALDATE");
            value.append(",TO_DATE('"+ DateUtil.parseDate(litigation.getDealdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
            //value.append(",'"+litigation.getDealdate()+"'");
        }
        if(litigation.getFirsthearingdate() != null && !"".equals(litigation.getFirsthearingdate())) {
            column.append(",FIRSTHEARINGDATE");
            value.append(",TO_DATE('"+ DateUtil.parseDate(litigation.getFirsthearingdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
           // value.append(",'"+litigation.getFirsthearingdate()+"'");
        }
        if(litigation.getLitigationenddate() != null && !"".equals(litigation.getLitigationenddate())) {
            column.append(",LITIGATIONENDDATE");
            value.append(",TO_DATE('"+ DateUtil.parseDate(litigation.getLitigationenddate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
           // value.append(",'"+litigation.getLitigationenddate()+"'");
        }
        if(litigation.getLitigationresult() != null && !"".equals(litigation.getLitigationresult())) {
            column.append(",LITIGATIONRESULT");
            value.append(",'"+litigation.getLitigationresult()+"'");
        }
        if(litigation.getCreatestaff() != null && !"".equals(litigation.getCreatestaff())) {
            column.append(",CREATESTAFF");
            value.append(",'"+litigation.getCreatestaff()+"'");
        }
//        if(litigation.getCreatetime() != null && !"".equals(litigation.getCreatetime())) {
//            column.append(",CREATETIME");
//            value.append(",'"+litigation.getCreatetime()+"'");
//        }
        if(litigation.getLinkorg() != null && !"".equals(litigation.getLinkorg())) {
            column.append(",LINKORG");
            value.append(",'"+litigation.getLinkorg()+"'");
        }
        if(litigation.getLitistatus() != null && !"".equals(litigation.getLitistatus())) {
            column.append(",LITISTATUS");
            value.append(",'"+litigation.getLitistatus()+"'");
        }
        if(litigation.getActionobject() != null && !"".equals(litigation.getActionobject())) {
            column.append(",ACTIONOBJECT");
            value.append(",'"+litigation.getActionobject()+"'");
        }
        if(litigation.getIseffect() != null && !"".equals(litigation.getIseffect())) {
            column.append(",ISEFFECT");
            value.append(",'"+litigation.getIseffect()+"'");
        }
        if(litigation.getJudgemoney() != null && !"".equals(litigation.getJudgemoney())) {
            column.append(",JUDGEMONEY");
            value.append(",'"+litigation.getJudgemoney()+"'");
        }
        if(litigation.getDisputeinfo() != null && !"".equals(litigation.getDisputeinfo())) {
            column.append(",DISPUTEINFO");
            value.append(",'"+litigation.getDisputeinfo()+"'");
        }

        column.append(")");
        value.append(")");
        String sql = column.toString()+value.toString();
        return sql;
    }


    public String updateModifyLitigationSettlement(TblLegalLitigationsettlement oldLitigation) {
        StringBuffer sql = new StringBuffer("UPDATE TBL_LEGAL_LITIGATIONSETTLEMENT SET FIRSTCOURT = '"+oldLitigation.getFirstcourt()+"'");

        if(oldLitigation.getPresidingjudge() != null && !"".equals(oldLitigation.getPresidingjudge())) {
            sql.append(" , PRESIDINGJUDGE = '"+oldLitigation.getPresidingjudge()+"'");
        }
        if(oldLitigation.getCollegialpanel() != null && !"".equals(oldLitigation.getCollegialpanel())) {
            sql.append(" , COLLEGIALPANEL = '"+oldLitigation.getCollegialpanel()+"'");
        }
        if(oldLitigation.getLitigationamount() != null && !"".equals(oldLitigation.getLitigationamount())) {
            sql.append(" , LITIGATIONAMOUNT = '"+oldLitigation.getLitigationamount()+"'");
        }
        if(oldLitigation.getDealdate() != null && !"".equals(oldLitigation.getDealdate())) {
            sql.append(" ,DEALDATE = TO_DATE('"+ DateUtil.parseDate(oldLitigation.getDealdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
        }
        if(oldLitigation.getFirsthearingdate() != null && !"".equals(oldLitigation.getFirsthearingdate())) {
            sql.append(" ,FIRSTHEARINGDATE = TO_DATE('"+ DateUtil.parseDate(oldLitigation.getFirsthearingdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
        }
        if(oldLitigation.getLitigationenddate() != null && !"".equals(oldLitigation.getLitigationenddate())) {
            sql.append(" ,LITIGATIONENDDATE = TO_DATE('"+ DateUtil.parseDate(oldLitigation.getFirsthearingdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
        }
        if(oldLitigation.getLitigationresult() != null && !"".equals(oldLitigation.getLitigationresult())) {
            sql.append(" , LITIGATIONRESULT = '"+oldLitigation.getLitigationresult()+"'");
        }
        if(oldLitigation.getCreatestaff() != null && !"".equals(oldLitigation.getCreatestaff())) {
            sql.append(" , CREATESTAFF = '"+oldLitigation.getCreatestaff()+"'");
        }
        if(oldLitigation.getCreatetime() != null && !"".equals(oldLitigation.getCreatetime())) {
            sql.append(" ,CREATETIME = TO_DATE('"+ DateUtil.parseDate(oldLitigation.getFirsthearingdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
        }
        if(oldLitigation.getLinkorg() != null && !"".equals(oldLitigation.getLinkorg())) {
            sql.append(" , LINKORG = '"+oldLitigation.getLinkorg()+"'");
        }
        if(oldLitigation.getLitistatus() != null && !"".equals(oldLitigation.getLitistatus())) {
            sql.append(" , LITISTATUS = '"+oldLitigation.getLitistatus()+"'");
        }
        if(oldLitigation.getActionobject() != null && !"".equals(oldLitigation.getActionobject())) {
            sql.append(" , ACTIONOBJECT = '"+oldLitigation.getActionobject()+"'");
        }
        if(oldLitigation.getIseffect() != null && !"".equals(oldLitigation.getIseffect())) {
            sql.append(" , ISEFFECT = '"+oldLitigation.getIseffect()+"'");
        }
        if(oldLitigation.getJudgemoney() != null && !"".equals(oldLitigation.getJudgemoney())) {
            sql.append(" , JUDGEMONEY = '"+oldLitigation.getJudgemoney()+"'");
        }
        if(oldLitigation.getDisputeinfo() != null && !"".equals(oldLitigation.getDisputeinfo())) {
            sql.append(" , DISPUTEINFO = '"+oldLitigation.getDisputeinfo()+"'");
        }

        sql.append(" WHERE LITIGATIONID = '"+oldLitigation.getLitigationid()+"'");
        return sql.toString();
    }
}
